package com.campus.controller;

import com.campus.pojo.SysArea;
import com.campus.service.SchoolService;
import com.campus.tools.Tree;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/school")
public class SchoolController {
    @Autowired
    SchoolService service;

    @RequestMapping("/getTreeList")
    @ResponseBody
    public Map<String,Object> getTreeList(){    //获取school tree
        List<Tree> treeList = service.getSchoolTree();
        Map<String,Object>result = new HashMap<>();
        result.put("zNodes",treeList);
        return result;
    }

    @ResponseBody
    @RequestMapping("/getSingleData")
    public Map<String,Object> getSingleData(@RequestParam("id") String areaNumber){
        //System.out.println(areaNumber);//调试
        SysArea area = service.getSelectedArea(areaNumber);
        String parentName = service.selectParentName(area.getParentId());
        if (parentName == null)
            parentName = "中国";
        area.setParentName(parentName);
        Map<String,Object> result = new HashMap<>();
        result.put("moduleData",area);
        return result;  
    }

    @ResponseBody
    @RequestMapping("/upSchool")
    public void updateSchool(SysArea area, HttpServletResponse response) throws IOException {   //更新院校
        JsonObject jsonObject = new JsonObject();
        ServletOutputStream out = response.getOutputStream();
        if(service.isRepeatName(area) > 0){//重名
            jsonObject.addProperty("exist","existName");
            out.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
            return;
        }
        int i = service.updateSchool(area);
        if (i>0){//修改成功
            jsonObject.addProperty("success",true);
        }else {
            jsonObject.addProperty("success",false);
        }
        out.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
    }

    @ResponseBody
    @RequestMapping("/addSchool")
    public void addSchool(HttpServletResponse response,SysArea area) throws IOException{
        JsonObject jsonObject = new JsonObject();
        ServletOutputStream out = response.getOutputStream();
        //主键重复
        if (service.isRepeatAreaNumber(area) > 0 ){
            jsonObject.addProperty("exist","existKey");
            out.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
            return;
        }
        //名称重复
        if (service.isRepeatName(area) > 0){
            jsonObject.addProperty("exist","existName");
            out.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
            return;
        }
        int i = service.addSchool(area);
        if (i>0){//修改成功
            jsonObject.addProperty("success",true);
        }else {
            jsonObject.addProperty("success",false);
        }
        out.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
    }

    @ResponseBody
    @RequestMapping("/delSchool")
    public void delSchool(String ids,HttpServletResponse response) throws IOException{
        JsonObject jsonObject = new JsonObject();
        ServletOutputStream out = response.getOutputStream();
        int i = service.delSchool(ids);
        if (i > 0){
            jsonObject.addProperty("success",true);
        }else {
            jsonObject.addProperty("success",false);
        }
        out.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
    }
}


